package com.bree.erp.jsf.view;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;

import org.apache.log4j.Logger;

import com.bree.erp.entity.PoItem;
import com.bree.erp.entity.PurchaseOrder;
import com.bree.erp.service.PurchaseOrderService;

@ManagedBean(name="dtPurchaseOrderItemsView")
@ViewScoped
public class PurchaseOrderItemsView {
	

	private static Logger LOG = Logger.getLogger(PurchaseOrderItemsView.class);
	
	@EJB 
	private PurchaseOrderService poService;
	
	private PurchaseOrder selectedItem;
	private Map<Long, PoItem> mapPoItem;
	private List<PoItem> poItems;
	private List<PoItem> fordeletion;
	private boolean dirty;
	
	public void selectPo(Long id) {
		
		LOG.debug("selected PO: " + id);
		
		selectedItem = poService.read(id);
		
		mapPoItem = new HashMap<Long, PoItem>();
		poItems = new ArrayList<PoItem>();
		if(selectedItem != null && selectedItem.getItems() != null) {
			int count = 0;
			for(PoItem poItem : poItems) {
				poItem.setItemNumber(++count);
				poItems.add(poItem);
				mapPoItem.put(poItem.getId(), poItem);
			}
		}
		
		LOG.debug("po items size: " + poItems.size());
		
	}
	
	private void recount() {
		
		if(poItems != null && !poItems.isEmpty()) {
			int count = 0;
			for(PoItem poItem : poItems) {
				poItem.setItemNumber(++count);
			}
		}
	}
	

	public void loadAsCollector(PurchaseOrder purchaseOrder) {
		poItems = new ArrayList<PoItem>(purchaseOrder.getItems());
		fordeletion = new ArrayList<PoItem>();
		
		recount();
	}
	
	public double total() {
		double total = 0.0;
		
		if(poItems != null && !poItems.isEmpty()) {
			for(PoItem poItem : poItems) {
				total += poItem.getTotalPrice();
			}
		}
		
		return total;
	}
	
	public List<PoItem> getItems() {
		return this.getPoItems();
	}
	
	public void deletePoItem(Long id) {
		
		PoItem template = new PoItem();
		template.setId(id);
		
		if(poItems.remove(template)) {
			fordeletion.add(template);
			recount();
			this.setDirty(true);
		}
		
	}
	

	/**
	 * @return the selectedItem
	 */
	public PurchaseOrder getSelectedItem() {
		return selectedItem;
	}

	/**
	 * @param selectedItem the selectedItem to set
	 */
	public void setSelectedItem(PurchaseOrder selectedItem) {
		this.selectedItem = selectedItem;
	}

	/**
	 * @return the mapPoItem
	 */
	public Map<Long, PoItem> getMapPoItem() {
		return mapPoItem;
	}

	/**
	 * @param mapPoItem the mapPoItem to set
	 */
	public void setMapPoItem(Map<Long, PoItem> mapPoItem) {
		this.mapPoItem = mapPoItem;
	}

	/**
	 * @return the poItems
	 */
	public List<PoItem> getPoItems() {
		return poItems;
	}

	/**
	 * @param poItems the poItems to set
	 */
	public void setPoItems(List<PoItem> poItems) {
		this.poItems = poItems;
	}


	/**
	 * @return the fordeletion
	 */
	public List<PoItem> getFordeletion() {
		return fordeletion;
	}

	/**
	 * @return the dirty
	 */
	public boolean isDirty() {
		return dirty;
	}

	/**
	 * @param dirty the dirty to set
	 */
	public void setDirty(boolean dirty) {
		this.dirty = dirty;
	}

}
